package de.codecasa.andro.kasse;

import java.math.BigDecimal;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;






public class NumKeypad {
	
	private Activity myActivity;
	private DisplayKlein myDisplay;

	private NumEventListener mListener;	


	Button btnNce;
	Button btnNBekommen;
	EditText txtNAnzahl;

	Button btnN0, btnN1, btnN2, btnN3, btnN4, btnN5, btnN6, btnN7, btnN8, btnN9;

	Button btnNKomma;

	Button btnN50er;
	Button btnN20er;
	Button btnN10er;	

	
	
	public enum Tasten {CE,N0,N1,N2,N3,N4,N5,N6,N7,N8,N9}
	

	public NumKeypad(Activity activity, DisplayKlein display)
	{
		myActivity = activity;
		myDisplay = display;
		initPad();
	}
	
	private void initPad()
	{
	    btnNce = (Button)myActivity.findViewById(R.id.btnNce);
	    btnNBekommen = (Button)myActivity.findViewById(R.id.btnNBekommen);
	    txtNAnzahl = (EditText)myActivity.findViewById(R.id.txtNAnzahl);
	    
	    btnN0 = (Button)myActivity.findViewById(R.id.btnN0);
	    btnN1 = (Button)myActivity.findViewById(R.id.btnN1);
	    btnN2 = (Button)myActivity.findViewById(R.id.btnN2);
	    btnN3 = (Button)myActivity.findViewById(R.id.btnN3);
	    btnN4 = (Button)myActivity.findViewById(R.id.btnN4);
	    btnN5 = (Button)myActivity.findViewById(R.id.btnN5);
	    btnN6 = (Button)myActivity.findViewById(R.id.btnN6);
	    btnN7 = (Button)myActivity.findViewById(R.id.btnN7);
	    btnN8 = (Button)myActivity.findViewById(R.id.btnN8);
	    btnN9 = (Button)myActivity.findViewById(R.id.btnN9);

	    btnNKomma = (Button)myActivity.findViewById(R.id.btnNKomma);

	    btnN50er = (Button)myActivity.findViewById(R.id.btnN50er);
	    btnN20er = (Button)myActivity.findViewById(R.id.btnN20er);
	    btnN10er = (Button)myActivity.findViewById(R.id.btnN10er);
	    

	    
	    
	    
	       btnNce.setOnClickListener(new OnClickListener() {
	        	@Override
	            public void onClick(View v) {
	            	// CE Event
	        		
	        		if(txtNAnzahl.getText().length() > 0 )
	        		{
	        			txtNAnzahl.setText("");
	        		}
	        		else
	        		{
	        			if(mListener != null) mListener.onTasteClick(Tasten.CE);
	        		}
	        			        		
	            }
	          });        

	        // Numpad Bekommen Klick
	        btnNBekommen.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	myDisplay.zahle(check_bekommen());
	            }
	          });        


	        
	        // Numpad 0
	        btnN0.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 1
	        btnN1.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 2
	        btnN2.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 3
	        btnN3.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 4
	        btnN4.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 5
	        btnN5.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 6
	        btnN6.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 7
	        btnN7.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 8
	        btnN8.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        // Numpad 9
	        btnN9.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	    

	        // Numpad Komma
	        btnNKomma.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	clickOperand(v);
	            }
	          });        
	        

	        // Numpad 50er
	        btnN50er.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	myDisplay.zahle(new BigDecimal("50.00"));
	            }
	          });        
	        // Numpad 20er
	        btnN20er.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	myDisplay.zahle(new BigDecimal("20.00"));
	            }
	          });        
	        // Numpad 10er
	        btnN10er.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	            	myDisplay.zahle(new BigDecimal("10.00"));
	            }
	          });        
	        
	        	    


	}

	
	public void clearNumpad()
	{
      	if(txtNAnzahl.getText().length() > 0 )
		{
			txtNAnzahl.setText("");
		}
    }
	
	
	
	
 	public int getAnzahl()
	{
		int Anzahl = 0;
		
		String aktText = txtNAnzahl.getText().toString();
		aktText.trim();
		
		if(!aktText.contains(",") && aktText.length() > 0)
		{
			int wert = Integer.parseInt(aktText);
			if(wert > 0)
			{
				Anzahl = wert;
			}
		}
		
		return Anzahl;
	}
 	
 	
	
	// Nummpad Click Operand
    private void clickOperand(View view) {
        Button button = (Button) view;
        String text = (String) button.getText();
        text = text.trim();

        String anzeige = txtNAnzahl.getText().toString();
        anzeige.trim();
        
        if(text.indexOf(",") > -1)
        {
        	if(anzeige.length() < 1 || anzeige.indexOf(",") > -1 )
        	{	
        		text = "";
        	}
        }
        
        txtNAnzahl.setText(txtNAnzahl.getText() + text);
    }		
	
	
	private BigDecimal check_bekommen()
	{
		BigDecimal rbd = new BigDecimal("0.00");
				
		String txtIn = txtNAnzahl.getText().toString();
		
		if(txtIn.length() > 0)
		{			
			String newStringToParse = txtIn.replace(",", ".");
			rbd = new BigDecimal(newStringToParse);
		}
		
		
		
		txtNAnzahl.setText("");
		return rbd;

		
	}    
    
    
	
	
	
	
	// Events
	
	public void setOnButtonClickEventListener(NumEventListener listener) 
    {
        mListener = listener;
    }    	
    

	public interface NumEventListener {
		void onTasteClick(Tasten button);
	}	
	
    
}
